package com.fernfx.xingtan.user.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.view.TextureView;
import android.widget.EditText;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.RongYun;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.main.ui.MainActivity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.user.contract.LoginContract;
import com.fernfx.xingtan.user.model.LoginModel;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.MobileInfoUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Login Presenter
 */
public class LoginPresenter<P extends LoginContract.View> implements LoginContract.Presenter,IRequestCallback{
    private P P;
    private LoginContract.Model model;


    @Override
    public String getSystemImei(Context context) {
        return MobileInfoUtil.getIMEI(context);
    }

    @Override
    public void init(BaseView P) {
        this.P = (P) P;
        model = new LoginModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        model.request(requestArgsMap,this);
    }

    @Override
    public void setLoginAccount(EditText userAccountEdit){
        if (AccountManager.isLogin() && null != AccountManager.getUserInfo()) {
            MemberEntity memberEntity = AccountManager.getUserInfo();
            String phoneNumber = memberEntity.getObj().getMobile();
            phoneNumber = TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber;
            userAccountEdit.setText(phoneNumber);
        }
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        P.getActivity().hideLoading();
        String message = result.getMessage();
        if (TextUtils.isEmpty(message)) {
            onNetworkError(result.getMessage(),500);onNetworkError(result.getMessage(),500);
            return;
        }
        MemberEntity memberEntity = FastJsonUtil.fromBean(message,MemberEntity.class);
        if (null != memberEntity && OtherUtil.checkRequestStatus(memberEntity.getSuccess(),memberEntity.getStatus())) {
            if (TextUtils.isEmpty(memberEntity.getObj().getHeadImg())){
                memberEntity.getObj().setHeadImg(Constant.ApplicationVariable.DEFAULT_USER_LOGO);
            }
            String rongYunToken = memberEntity.getObj().getRongCloudToken();
            if (TextUtils.isEmpty(rongYunToken)) {
                ToastUtil.showCentertoast("融云Token为空");
                return;
            }
            //连接融云服务器
            RongYun.connectRongYun(P.getActivity(),memberEntity,rongYunToken,true);
        }else {
            ToastUtil.showCentertoast(memberEntity.getMsg());
        }
    }

    @Override
    public void onFailure(String errorMessage, int errorCode) {
        P.getActivity().hideLoading();
        if (TextUtils.isEmpty(errorMessage)) {
            onNetworkError(errorMessage,errorCode);
            return;
        }

        final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
        if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
            ToastUtil.showCentertoast(baseEntity.getMsg());
        }
    }

    @Override
    public void onNetworkError(String errorMessage, int errorCode) {
        P.getActivity().hideLoading();
        ToastUtil.showCentertoast(R.string.network_error);
    }
}
